package com.shoe.controller;

import com.shoe.pojo.ShoeUser;
import com.shoe.service.ShoeUserService;
import com.shoe.service.UserService;
import com.shoe.pojo.User;
import com.shoe.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.UUID;

@Controller
@CrossOrigin
public class UserController {

    @Autowired
    ShoeUserService shoeUserService;

    public Integer selectOne(){

        return 0;
    }

    @RequestMapping("selectUser")
    @ResponseBody
    public ShoeUser selectUser(@RequestParam String userId){
       ShoeUser shoeUser = shoeUserService.selectUser(Long.valueOf(userId));

        return shoeUser;
    }


    @RequestMapping("updateUser/{userId}")
    public String updateUser(@PathVariable String userId, String userSex, String userAddress , String userMail,String userPhone,String userImg,String userCard ,String reserve2 ,String reserve3){
        ShoeUser shoeUser = new ShoeUser();

        shoeUser.setUserId(Long.valueOf(userId));

        if(userSex != "null"){
            shoeUser.setUserSex(userSex);
        }
        if(userAddress != "null" && userAddress != ""){
            shoeUser.setUserAddress(userAddress);
        }
        if(userMail != "null"){
            shoeUser.setUserMail(userMail);
        }
        if(userPhone != "null"){
            shoeUser.setUserPhone(userPhone);
        }
        if(userImg != "null"){
            shoeUser.setUserImg(userImg);
        }
        if(userCard != "null"){
            shoeUser.setUserCard(userCard);
        }

        if(reserve2  != "null"){
            shoeUser.setReserve2(reserve2);
        }

        if(reserve3  != "null"){
            shoeUser.setReserve3(reserve3);
        }


       int b = shoeUserService.updataUser(shoeUser);

        if(b >= 0){
            return "redirect:http://localhost:63343/qiguai/html/person.html";
        }

        return null;
    }

    @PostMapping("clip/{userId}")
    @ResponseBody
    public ResponseResult addPhoto(HttpServletRequest request, HttpServletResponse response,@PathVariable String userId) throws IOException, ServletException {
        Collection<Part> collection = request.getParts();
        ShoeUser shoeUser = new ShoeUser();
        if(null != collection){
            Iterator<Part> iterator = collection.iterator();
            while (iterator.hasNext()){
                Part part = iterator.next();
                String fileName = part.getSubmittedFileName();
                ResponseResult responseResult = new ResponseResult();
                if(null != fileName){
                    // 保存的最终文件名
                    String finalFileName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));// .jpg  .docx  .txt
                    // 把文件保存到对应的路径去----将文件上传后的最终路径保存到数据库
                    System.out.println(finalFileName);
                    part.write("H:\\project\\photo\\" + finalFileName);
                    shoeUser.setUserImg(finalFileName);
                    shoeUser.setUserId(Long.valueOf(userId));
                    shoeUserService.updataUser(shoeUser);
                    responseResult.setCode(1);
                    responseResult.setMsg("上传成功");
                    return responseResult;
                }else {
                    responseResult.setCode(0);
                    responseResult.setMsg("上传失败");
                    return responseResult;
                }
            }
        }
        return null;
    }




}
